﻿Imports Comercializacion_BE
Imports Comercializacion_BL
Imports System.Windows.Forms

Public Class frm_metales_preciosos
    Dim nopc As Integer = 0
    Dim ocodmet As New metales_preciososBL
    Dim ometprecioso As New metales_preciososBE


    Private Sub tsAgregar_Click(sender As System.Object, e As System.EventArgs) Handles tsAgregar.Click

        DeshabilitarMenu()
        HabilitarCampos()

        txtCodigoMetal.Text = ""
        Dim vcodigometal As String = ocodmet.ObtenerCodigoMetal()
        txtCodigoMetal.Text = vcodigometal

        txtSimbolo.Text = " "
        txtNombre.Text = " "

        HabilitarBotones()

        nopc = 1

    End Sub

    Private Sub HabilitarBotones()

        btnGuardar.Enabled = True
        btnCancelar.Enabled = True

    End Sub


    Private Sub HabilitarCampos()

        txtCodigoMetal.Enabled = False
        txtSimbolo.Enabled = True
        txtNombre.Enabled = True

    End Sub

    Private Sub frm_metales_preciosos_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        LlenarGrilla()
        DeshabilitarBotones()

    End Sub


    Private Sub LlenarGrilla()

        Dim mpGrid As New metales_preciososBL
        gcMetalesPreciosos.DataSource = mpGrid.ObtenerMetalesPreciososGrid()


    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click

        DeshabilitarCampos()
        HabilitarMenu()
        DeshabilitarBotones()
        txtCodigoMetal.Text = " "
        txtSimbolo.Text = " "
        txtNombre.Text = " "

    End Sub

    Private Sub DeshabilitarCampos()

        txtCodigoMetal.Enabled = False
        txtSimbolo.Enabled = False
        txtNombre.Enabled = False

    End Sub

    Private Sub HabilitarMenu()

        tsAgregar.Enabled = True
        tsModificar.Enabled = True
        tsEliminar.Enabled = True

    End Sub

    Private Sub DeshabilitarBotones()

        btnGuardar.Enabled = False
        btnCancelar.Enabled = False

    End Sub

    Private Sub DeshabilitarMenu()

        tsAgregar.Enabled = False
        tsModificar.Enabled = False
        tsEliminar.Enabled = False

    End Sub


    Private Sub GroupBox3_Enter(sender As System.Object, e As System.EventArgs) Handles GroupBox3.Enter

    End Sub

    Private Sub gcMetalesPreciosos_Click(sender As System.Object, e As System.EventArgs) Handles gcMetalesPreciosos.Click

    End Sub

    Private Sub GridView1_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles GridView1.FocusedRowChanged

        If GridView1.RowCount() Then
            FocusedFila()
        End If

    End Sub

    Private Sub FocusedFila()

        DeshabilitarCampos()
        Dim Rows As New ArrayList()
        Rows.Add(GridView1.GetDataRow(GridView1.GetSelectedRows()(0)))
        Dim Row As DataRow = CType(Rows(0), DataRow)
        If Not Row Is Nothing Then
            txtCodigoMetal.Text = Row("codigometal").ToString()
            txtSimbolo.Text = Row("simbolo").ToString()
            txtNombre.Text = Row("nombre").ToString()
        End If

    End Sub


    Private Sub btnGuardar_Click(sender As System.Object, e As System.EventArgs) Handles btnGuardar.Click


        Dim met As New metales_preciososBL

        ometprecioso.codigometal = txtCodigoMetal.Text
        ometprecioso.simbolo = txtSimbolo.Text
        ometprecioso.nombre = txtNombre.Text

        Select Case nopc
            Case 1
                If met.InsertarMetalesPreciosos(ometprecioso) Then
                    MessageBox.Show("El nuevo metal se ha guardado con exito")
                    LlenarGrilla()
                    DeshabilitarCampos()
                    HabilitarMenu()
                    DeshabilitarBotones()
                Else
                    MessageBox.Show("Hubo errores. No se pudo guardar el metal")
                    DeshabilitarCampos()
                    HabilitarMenu()
                    DeshabilitarBotones()
                End If
            Case 2
                If met.ModificarMetalesPreciosos(ometprecioso) Then
                    MessageBox.Show("El metal ha sido modificado con exito")
                    LlenarGrilla()
                    DeshabilitarCampos()
                    HabilitarMenu()
                    DeshabilitarBotones()
                Else
                    MessageBox.Show("Hubo errores. No se pudo modificar el metal")
                    DeshabilitarCampos()
                    HabilitarMenu()
                    DeshabilitarBotones()
                End If
        End Select

    End Sub

    Private Sub tsModificar_Click(sender As System.Object, e As System.EventArgs) Handles tsModificar.Click

        HabilitarCampos()
        DeshabilitarMenu()
        HabilitarBotones()
        nopc = 2

    End Sub

    Private Sub tsSalir_Click(sender As System.Object, e As System.EventArgs) Handles tsSalir.Click

        Me.Close()

    End Sub

    Private Sub tsEliminar_Click(sender As System.Object, e As System.EventArgs) Handles tsEliminar.Click

        'Dim mp As New metales_preciososBE
        Dim met As New metales_preciososBL
        ometprecioso.codigometal = txtCodigoMetal.Text
        ometprecioso.nombre = txtNombre.Text

        If MsgBox("¿Está seguro de eliminar el metal (" & ometprecioso.codigometal & "/" & ometprecioso.nombre & ")?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Decisión") = MsgBoxResult.Yes Then
            met.EliminarMetalesPreciosos(ometprecioso)
            LlenarGrilla()
        End If

    End Sub
End Class